package com.yswy.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yswy.app.dto.domianVo.PackageManageVo;
import com.yswy.app.service.PackageManageService;
import com.yswy.common.utils.StringUtils;
import com.yswy.common.utils.redis.RedisConstants;
import com.yswy.common.utils.redis.RedisUtil;
import com.yswy.domain.entity.PackageManage;
import com.yswy.domain.entity.TgChannleChild;
import com.yswy.domain.mapper.PackageManageMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;


@Slf4j
@Service
public class PackageManageServiceImpl extends ServiceImpl<PackageManageMapper, PackageManage>
        implements PackageManageService {

    @Autowired
    private RedisUtil redisUtil ;


    @Override
    public PackageManageVo gePackageManage(String channel) {

        PackageManage packageManage = null ;
        String key = RedisConstants.packUpdate+channel ;
        if (redisUtil.hasKey(key)){
            packageManage = (PackageManage)redisUtil.getObejct(key);
        }else {
            packageManage = getOne(new LambdaQueryWrapper<PackageManage>()
                    .eq(PackageManage::getStatus , 0)
                    .eq(PackageManage::getIsDel , 0)
                    .ne(PackageManage::getIsForce , 2)
                    .eq(StringUtils.isNotEmpty(channel) ,PackageManage::getChannel , channel)
                    .orderByDesc(PackageManage::getVersion )
                    .last("limit  1"));
        }
        if (packageManage!= null){
            redisUtil.setForObjectTimeCustom(key, packageManage, 1, TimeUnit.DAYS);
            return  new PackageManageVo(packageManage);
        }else {
            return null ;
        }
    }

    @Override
    public PackageManage gePackageManageByChannle(String channel) {
        PackageManage packageManage = null ;
        if (StringUtils.isEmpty(channel)){
            channel = "android";
        }
        String key = RedisConstants.packChannle+channel ;
        if (redisUtil.hasKey(key)){
            packageManage = (PackageManage)redisUtil.getObejct(key);
        }else {
            packageManage = getOne(new LambdaQueryWrapper<PackageManage>()
                            .eq(PackageManage::getIsForce , 2)
                            .eq(PackageManage::getChannel , channel)
                            .orderByDesc(PackageManage::getVersion )
                            .last("limit  1"));
        }

        if (packageManage == null){
            key = RedisConstants.packChannle+"android" ;
            if (redisUtil.hasKey(key)){
                packageManage = (PackageManage)redisUtil.getObejct(key);
            }else {
                packageManage = getOne(new LambdaQueryWrapper<PackageManage>()
                        .eq(PackageManage::getIsForce , 2)
                        .eq(PackageManage::getChannel , "android")
                        .orderByDesc(PackageManage::getVersion )
                        .last("limit  1"));
            }
        }

        if (packageManage!= null){
            redisUtil.setForObjectTimeCustom(key, packageManage, 1, TimeUnit.DAYS);
        }
        return  packageManage;
    }
    @Override
    public Boolean isCallBackDisposable(String channle) {
        PackageManage packageManage = null ;
        String key = RedisConstants.packChannle+channle ;
        if (redisUtil.hasKey(key)){
            packageManage = (PackageManage)redisUtil.getObejct(key);
        }else {
            packageManage = getOne(new LambdaQueryWrapper<PackageManage>()
                    .eq(PackageManage::getIsForce , 2)
                    .eq(PackageManage::getChannel , channle)
                    .orderByDesc(PackageManage::getVersion )
                    .last("limit  1"));
        }
        if (packageManage!=null){
            return packageManage.getIsCallBackDisposable()==1?true:false;
        }else {
            return false ;
        }
    }

    @Override
    public Integer showVipWindow(String channle) {
        PackageManage packageManage = null ;
        String key = RedisConstants.packChannle+channle ;
        if (redisUtil.hasKey(key)){
            packageManage = (PackageManage)redisUtil.getObejct(key);
        }else {
            packageManage = getOne(new LambdaQueryWrapper<PackageManage>()
                    .eq(PackageManage::getIsForce , 2)
                    .eq(PackageManage::getChannel , channle)
                    .orderByDesc(PackageManage::getVersion )
                    .last("limit  1"));
        }
        if (packageManage!=null){
            return packageManage.getShowVipWindow();
        }else {
            return 0 ;
        }
    }

    @Override
    public void pdPackageId(String packId) {

        if (StringUtils.isEmpty(packId)){
            throw new IllegalArgumentException("当前版本过低,请联系客服升级");
        }
        PackageManage packageManage = getOne(new LambdaQueryWrapper<PackageManage>()
                .eq(PackageManage::getStatus , 0)
                .orderByDesc(PackageManage::getVersion )
                .last("limit  1"));
        if (packageManage != null){
            String versionCode = packageManage.getVersionCode().replace(".","");
            if (!versionCode.equals(packId)){
                throw new IllegalArgumentException("当前版本过低,请联系客服升级");
            }
        }
    }



}
